package com.aaa.controller;

import com.aaa.pojo.Menu;
import com.aaa.pojo.UserInfo;
import com.aaa.service.MenuService;
import com.aaa.service.UserService;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import java.lang.reflect.Array;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @Classname UserAuthorityController
 * @Description TODO
 * @Date 2020/12/13 21:37
 * @Created by 腾飞
 */
@Controller
public class UserAuthorityController {
    @Autowired
    private UserService userService;
    @Autowired
    private MenuService menuService;

    @RequestMapping("/userAuthority")
    @ResponseBody
    public ResponseEntity<Map<String,Object>> userAuthority(){
        Map<String,Object> map = new HashMap<>();
        List<UserInfo> userInfos = userService.queryUserAll();
        List<UserInfo> after = new ArrayList<>();
        String Visits[];
        for(UserInfo user: userInfos){
            Visits = user.getVisit().split(" ");
            Integer[] result = new Integer[Visits.length];//int类型数组
            for(int i=0;i<Visits.length;i++){
                result[i] = Integer.parseInt(Visits[i]);//整数数组
            }
            user.setVisits(result);
            after.add(user);
        }
        List<Menu> menus = menuService.queryMenuAll(1);
        map.put("User",after);
        map.put("menulist",menus);
        return new ResponseEntity<Map<String,Object>>(map,HttpStatus.OK);
    }
    @RequestMapping("/updateAuthority")
    @ResponseBody
    public ResponseEntity<Map<String,Object>> updateAuthority(@RequestBody JSONObject jsonObject){
        Integer uid = jsonObject.getInteger("uid");
        JSONArray authoritys = jsonObject.getJSONArray("authoritys");
        Map<String,Object> map = new HashMap<>();
        String auths = "1";
        for(Object auth : authoritys){
            auths+=" "+auth;
        }
        System.out.println("-=-=-"+auths);
        userService.updateVisits(auths,uid);
        return new ResponseEntity<Map<String,Object>>(map,HttpStatus.OK);
    }
}
